
package com.chongchong.user.feign;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chongchong.user.entity.User;
import com.chongchong.user.entity.UserInfo;
import com.chongchong.user.entity.UserOauth;
import com.chongchong.system.entity.Role;
import com.chongchong.system.service.IRoleService;
import com.chongchong.user.service.IUserService;
import lombok.AllArgsConstructor;
import com.chongchong.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户服务Feign实现类
 *
 * @author
 */
@RestController
@AllArgsConstructor
public class UserClient implements IUserClient {

	private IUserService service;
	private IRoleService roleService;

	@Override
	public R<UserInfo> userInfo(Long userId) {
		return R.data(service.userInfo(userId));
	}

	@Override
	@GetMapping(API_PREFIX + "/user-info")
	public R<UserInfo> userInfo(String tenantId, String account, String password) {
		return R.data(service.userInfo(tenantId, account, password));
	}
	@Override
	@GetMapping(API_PREFIX + "/user-info-account")
	public R<UserInfo> userInfo(String tenantId, String account) {
		return R.data(service.userInfo(tenantId, account));
	}
	@Override
	@PostMapping(API_PREFIX + "/user-auth-info")
	public R<UserInfo> userAuthInfo(UserOauth userOauth) {
		return R.data(service.userInfo(userOauth));
	}

	@Override
	@PostMapping(API_PREFIX + "/save-user")
	public R<Boolean> saveUser(User user) {
		return R.data(service.save(user));
	}
	@Override
	@PostMapping(API_PREFIX + "/update-user")
	public R<Boolean> updateUser(User user) {
		return R.data(service.submit(user));
	}

	@Override
	@PostMapping(API_PREFIX + "/save-or-update-userlist")
	public R<Boolean> saveOrUpdateUserList(@RequestBody List<User> userList) {
		return R.data(service.saveOrUpdateBatch(userList,userList.size()));
	}

	@Override
	public R<List<User>> findByRoleName(String roleName) {
		Role role = new Role();
		role.setRoleName(roleName);
		Wrapper<Role> queryWrapper = new QueryWrapper<>(role);
		role = roleService.getOne(queryWrapper);
		List<User> userList = new ArrayList<>();
		if(role != null){
			userList = service.getUserByRoleIds(String.valueOf(role.getId()));
		}
		return R.data(userList);
	}
}
